﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RATSharpW7.ViewModel;
using RATSharp.Settings;

namespace RATSharp.Model
{
    /// <summary>
    /// Do a mapping for Model which will be serialised
    /// </summary>
    public class ReservationFactory
    {
        /// <summary>
        /// Create new Reservation collection
        /// </summary>
        /// <param name="since">period since</param>
        /// <param name="to">period to</param>
        /// <param name="numberOfWeeks">nember of weeks</param>
        /// <param name="obj">object to reserve</param>
        /// <param name="pickAction">is reservation already picked?</param>
        /// <returns></returns>
        public static Reservations createReservations(DateTime since, DateTime to, int numberOfWeeks, Model.Object obj, PickAction pickAction)
        {
            Reservations reservations = new Reservations();
            Reservation reservation = new Reservation();
            reservations.Add(reservation);
            reservation.objectId = obj.id;
            reservation.objectName = obj.name;
            reservation.Periodical = numberOfWeeks > 1;
            reservation.PeriodCount = numberOfWeeks;
            reservation.periods = new List<period>();
            for (int i = 0; i < numberOfWeeks; i++)
            {
                period period = new period();
                period.FromDate = since;

                period.ToDate = to;
                period.Valid = true;
                period.pickAction = pickAction;
                period.returnAction = ReturnAction.NOT_RETURNED;

                reservation.periods.Add(period);
                since = since.AddDays(7);
                to = to.AddDays(7);
            }
            return reservations;
        }

        public static void FirstAndLastDayOfMonth(DateTime date, out DateTime first, out DateTime last)
        {
            first = new DateTime(date.Year, date.Month, 1);
            DateTime nextFirst;
            if (first.Month == 12) nextFirst = new DateTime(first.Year + 1, 1, 1);
            else nextFirst = new DateTime(first.Year, first.Month + 1, 1);
            last = nextFirst.AddDays(-1);
        }

        /// <summary>
        /// This is a little bit hack but it will make data handling easier
        /// (and also it is working in the same way on server)
        /// </summary>
        /// <param name="reservations">reservations</param>
        public static void MakeBidirectionalConnection(Reservations reservations)
        {
            foreach (Reservation res in reservations)
            {
                foreach (period period in res.periods)
                {
                    period.reservation = res;
                }
            }
        }


        //create new Dummy model which we can mask as our new reservation
        public static period createNewReservation(CreateReservationData data, string username)
        {
            Reservation reservation = new Reservation();
            // at least one is always set
            reservation.PeriodCount = 1;
            reservation.objectName = data.obj.name;
            reservation.userName = username;
            reservation.objectId = data.obj.id;
            reservation.userName = username;
            reservation.Periodical = false;

            List<period> periods = new List<period>();
            period period = new period();
            DateTime selectedDate = data.selectedDate;

            selectedDate = selectedDate.Date.Add(DateTime.Now.Date.Equals(selectedDate.Date) ? DateTime.Now.TimeOfDay : new TimeSpan(12, 0, 0));
            period.FromDate = selectedDate;
            long minimumMinutesDuration = AppSettings.Setting.PropertiesSettings.minimumDuration;
            period.ToDate = selectedDate.AddHours((new TimeSpan(0,(int)minimumMinutesDuration, 0)).Hours);
            period.reservation = reservation;
            period.Valid = true;
            period.pickAction = data.pickAction;
            period.returnAction = data.obj.returnable ? ReturnAction.NOT_RETURNED : ReturnAction.NOT_RETURNABLE;

            periods.Add(period);
            reservation.periods = periods;
            return period;
        }

        public class CreateReservationData
        {
            public RATSharp.Model.Object obj { get; set; }
            public DateTime selectedDate { get; set; }
            public PickAction pickAction { get; set; }
        }
    }
}
